<!DOCTYPE html>

<html>
<head>
  <title>accounts.js</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="arkio.css" />
</head>
<body>
  
    <div id="title">
         <h1>accounts.js</h1>
         <img src="https://ark.io/wp-content/uploads/2016/10/ark-normal.png">
        <hr>
    </div>
  
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page_wrapper">
            <div id="jump_page">
              
                
                <a class="source" href="README.html">
                  README.md
                </a>
              
                
                <a class="source" href="app.html">
                  app.js
                </a>
              
                
                <a class="source" href="accounts.html">
                  accounts.js
                </a>
              
                
                <a class="source" href="blockchain.html">
                  blockchain.js
                </a>
              
                
                <a class="source" href="blocks.html">
                  blocks.js
                </a>
              
                
                <a class="source" href="delegates.html">
                  delegates.js
                </a>
              
                
                <a class="source" href="loader.html">
                  loader.js
                </a>
              
                
                <a class="source" href="multisignatures.html">
                  multisignatures.js
                </a>
              
                
                <a class="source" href="nodeManager.html">
                  nodeManager.js
                </a>
              
                
                <a class="source" href="peers.html">
                  peers.js
                </a>
              
                
                <a class="source" href="rounds.html">
                  rounds.js
                </a>
              
                
                <a class="source" href="server.html">
                  server.js
                </a>
              
                
                <a class="source" href="signatures.html">
                  signatures.js
                </a>
              
                
                <a class="source" href="system.html">
                  system.js
                </a>
              
                
                <a class="source" href="transactionPool.html">
                  transactionPool.js
                </a>
              
                
                <a class="source" href="transactions.html">
                  transactions.js
                </a>
              
                
                <a class="source" href="transport.html">
                  transport.js
                </a>
              
            </div>
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
        
        <li id="section-1">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-meta">'use strict'</span>;

<span class="hljs-keyword">var</span> bignum = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../helpers/bignum.js'</span>);
<span class="hljs-keyword">var</span> BlockReward = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../logic/blockReward.js'</span>);
<span class="hljs-keyword">var</span> constants = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../helpers/constants.js'</span>);
<span class="hljs-keyword">var</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
<span class="hljs-keyword">var</span> arkjs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'arkjs'</span>);
<span class="hljs-keyword">var</span> extend = <span class="hljs-built_in">require</span>(<span class="hljs-string">'extend'</span>);
<span class="hljs-keyword">var</span> Router = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../helpers/router.js'</span>);
<span class="hljs-keyword">var</span> schema = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../schema/accounts.js'</span>);
<span class="hljs-keyword">var</span> slots = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../helpers/slots.js'</span>);
<span class="hljs-keyword">var</span> transactionTypes = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../helpers/transactionTypes.js'</span>);</pre></div></div>
            
        </li>
        
        
        <li id="section-2">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Private fields</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> modules, library, self, __private = {}, shared = {};

__private.assetTypes = {};
__private.blockReward = <span class="hljs-keyword">new</span> BlockReward();</pre></div></div>
            
        </li>
        
        
        <li id="section-3">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Constructor</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Accounts</span> (<span class="hljs-params">cb, scope</span>) </span>{
	library = scope;
	self = <span class="hljs-keyword">this</span>;

	<span class="hljs-keyword">var</span> Vote = <span class="hljs-built_in">require</span>(<span class="hljs-string">'../logic/vote.js'</span>);
	__private.assetTypes[transactionTypes.VOTE] = library.logic.transaction.attachAssetType(
		transactionTypes.VOTE, <span class="hljs-keyword">new</span> Vote()
	);

	setImmediate(cb, <span class="hljs-literal">null</span>, self);
}</pre></div></div>
            
        </li>
        
        
        <li id="section-4">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>Private methods</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>__private.attachApi = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
	<span class="hljs-keyword">var</span> router = <span class="hljs-keyword">new</span> Router();

	router.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
		<span class="hljs-keyword">if</span> (modules) { <span class="hljs-keyword">return</span> next(); }
		res.status(<span class="hljs-number">500</span>).send({<span class="hljs-attr">success</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">error</span>: <span class="hljs-string">'Blockchain is loading'</span>});
	});

	router.map(shared, {
		<span class="hljs-string">'get /getBalance'</span>: <span class="hljs-string">'getBalance'</span>,
		<span class="hljs-string">'get /getPublicKey'</span>: <span class="hljs-string">'getPublickey'</span>,
		<span class="hljs-string">'get /delegates'</span>: <span class="hljs-string">'getDelegates'</span>,
		<span class="hljs-string">'get /delegates/fee'</span>: <span class="hljs-string">'getDelegatesFee'</span>,
		<span class="hljs-string">'put /delegates'</span>: <span class="hljs-string">'addDelegates'</span>,
		<span class="hljs-string">'get /'</span>: <span class="hljs-string">'getAccount'</span>
	});

	<span class="hljs-keyword">if</span> (process.env.DEBUG &amp;&amp; process.env.DEBUG.toUpperCase() === <span class="hljs-string">'TRUE'</span>) {
		router.get(<span class="hljs-string">'/getAllAccounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
			<span class="hljs-keyword">return</span> res.json({<span class="hljs-attr">success</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">accounts</span>: __private.accounts});
		});
	}

	<span class="hljs-keyword">if</span> (process.env.TOP &amp;&amp; process.env.TOP.toUpperCase() === <span class="hljs-string">'TRUE'</span>) {
		router.get(<span class="hljs-string">'/top'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
			req.sanitize(req.query, schema.top, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, report, query</span>) </span>{
				<span class="hljs-keyword">if</span> (err) { <span class="hljs-keyword">return</span> next(err); }
				<span class="hljs-keyword">if</span> (!report.isValid) { <span class="hljs-keyword">return</span> res.json({<span class="hljs-attr">success</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">error</span>: report.issues}); }

				self.getAccounts({
					<span class="hljs-attr">sort</span>: {
						<span class="hljs-attr">balance</span>: <span class="hljs-number">-1</span>
					},
					<span class="hljs-attr">offset</span>: query.offset,
					<span class="hljs-attr">limit</span>: (query.limit || <span class="hljs-number">100</span>)
				}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, raw</span>) </span>{
					<span class="hljs-keyword">if</span> (err) {
						<span class="hljs-keyword">return</span> res.json({<span class="hljs-attr">success</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">error</span>: err});
					}

					<span class="hljs-keyword">var</span> accounts = raw.map(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">account</span>) </span>{
						<span class="hljs-keyword">return</span> {
							<span class="hljs-attr">address</span>: account.address,
							<span class="hljs-attr">balance</span>: account.balance,
							<span class="hljs-attr">publicKey</span>: account.publicKey
						};
					});

					res.json({<span class="hljs-attr">success</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">accounts</span>: accounts});
				});
			});
		});
	}

	router.get(<span class="hljs-string">'/count'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
		<span class="hljs-keyword">return</span> res.json({<span class="hljs-attr">success</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">count</span>: <span class="hljs-built_in">Object</span>.keys(__private.accounts).length});
	});

	router.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res, next</span>) </span>{
		res.status(<span class="hljs-number">500</span>).send({<span class="hljs-attr">success</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">error</span>: <span class="hljs-string">'API endpoint was not found'</span>});
	});

	library.network.app.use(<span class="hljs-string">'/api/accounts'</span>, router);
	library.network.app.use(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, req, res, next</span>) </span>{
		<span class="hljs-keyword">if</span> (!err) { <span class="hljs-keyword">return</span> next(); }
		library.logger.error(<span class="hljs-string">'API error '</span> + req.url, err);
		res.status(<span class="hljs-number">500</span>).send({<span class="hljs-attr">success</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">error</span>: <span class="hljs-string">'API error: '</span> + err.message});
	});
};</pre></div></div>
            
        </li>
        
        
        <li id="section-5">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>Public methods</p>
<p><strong>API</strong> <code>generateAddressByPublicKey</code></p>

            </div>
            
        </li>
        
        
        <li id="section-6">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Accounts.prototype.generateAddressByPublicKey = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">publicKey</span>) </span>{
	<span class="hljs-keyword">return</span> arkjs.crypto.getAddress(publicKey);
};</pre></div></div>
            
        </li>
        
        
        <li id="section-7">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p><strong>API</strong> <code>getAccount</code></p>

            </div>
            
        </li>
        
        
        <li id="section-8">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Accounts.prototype.getAccount = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">filter, fields, cb</span>) </span>{
	<span class="hljs-keyword">if</span> (filter.publicKey) {
		filter.address = self.generateAddressByPublicKey(filter.publicKey);
		<span class="hljs-keyword">delete</span> filter.publicKey;
	}

	library.logic.account.get(filter, fields, cb);
};</pre></div></div>
            
        </li>
        
        
        <li id="section-9">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p><strong>API</strong> <code>getAccounts</code></p>

            </div>
            
        </li>
        
        
        <li id="section-10">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Accounts.prototype.getAccounts = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">filter, fields, cb</span>) </span>{
	library.logic.account.getAll(filter, fields, cb);
};</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p><strong>API</strong> <code>setAccountAndGet</code></p>

            </div>
            
        </li>
        
        
        <li id="section-12">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Accounts.prototype.setAccountAndGet = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data, cb</span>) </span>{
	<span class="hljs-keyword">var</span> address = data.address || <span class="hljs-literal">null</span>;

	<span class="hljs-keyword">if</span> (address === <span class="hljs-literal">null</span>) {
		<span class="hljs-keyword">if</span> (data.publicKey) {
			address = self.generateAddressByPublicKey(data.publicKey);
		} <span class="hljs-keyword">else</span> {
			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Missing address or public key'</span>);
		}
	}

	<span class="hljs-keyword">if</span> (!address) {
		<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid public key'</span>);
	}

	library.logic.account.set(address, data, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err);
		}
		<span class="hljs-keyword">return</span> library.logic.account.get({<span class="hljs-attr">address</span>: address}, cb);
	});
};</pre></div></div>
            
        </li>
        
        
        <li id="section-13">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p><strong>API</strong> <code>mergeAccountAndGet</code></p>

            </div>
            
        </li>
        
        
        <li id="section-14">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Accounts.prototype.mergeAccountAndGet = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data, cb</span>) </span>{
	<span class="hljs-keyword">var</span> address = data.address || <span class="hljs-literal">null</span>;

	<span class="hljs-keyword">if</span> (address === <span class="hljs-literal">null</span>) {
		<span class="hljs-keyword">if</span> (data.publicKey) {
			address = self.generateAddressByPublicKey(data.publicKey);
		} <span class="hljs-keyword">else</span> {
			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Missing address or public key'</span>);
		}
	}

	<span class="hljs-keyword">if</span> (!address) {
		<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid public key'</span>);
	}

	<span class="hljs-keyword">return</span> library.logic.account.merge(address, data, cb);
};</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <p>Events</p>
<p><strong>EVENT</strong> <code>onBind</code></p>

            </div>
            
        </li>
        
        
        <li id="section-16">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Accounts.prototype.onBind = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">scope</span>) </span>{
	modules = scope;

	__private.assetTypes[transactionTypes.VOTE].bind({
		<span class="hljs-attr">modules</span>: modules, <span class="hljs-attr">library</span>: library
	});
};</pre></div></div>
            
        </li>
        
        
        <li id="section-17">
          
            <div class="fullblock">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p><strong>EVENT</strong> <code>onAttachPublicApi</code></p>

            </div>
            
        </li>
        
        
        <li id="section-18">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>Accounts.prototype.onAttachPublicApi = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
 	__private.attachApi();
};



shared.getBalance = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	library.schema.validate(req.body, schema.getBalance, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err[<span class="hljs-number">0</span>].message);
		}

		<span class="hljs-keyword">var</span> isAddress = <span class="hljs-regexp">/^[1-9A-Za-z]{1,35}$/g</span>;
		<span class="hljs-keyword">if</span> (!isAddress.test(req.body.address)) {
			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid address'</span>);
		}

		self.getAccount({ <span class="hljs-attr">address</span>: req.body.address }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, account</span>) </span>{
			<span class="hljs-keyword">if</span> (err) {
				<span class="hljs-keyword">return</span> setImmediate(cb, err);
			}

			<span class="hljs-keyword">var</span> balance = account ? account.balance : <span class="hljs-string">'0'</span>;
			<span class="hljs-keyword">var</span> unconfirmedBalance = account ? account.u_balance : <span class="hljs-string">'0'</span>;

			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">balance</span>: balance, <span class="hljs-attr">unconfirmedBalance</span>: unconfirmedBalance});
		});
	});
};

shared.getPublickey = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	library.schema.validate(req.body, schema.getPublicKey, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err[<span class="hljs-number">0</span>].message);
		}

		<span class="hljs-keyword">var</span> isAddress = <span class="hljs-regexp">/^[1-9A-Za-z]{1,35}$/g</span>;
		<span class="hljs-keyword">if</span> (!isAddress.test(req.body.address)) {
			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid address'</span>);
		}

		self.getAccount({ <span class="hljs-attr">address</span>: req.body.address }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, account</span>) </span>{
			<span class="hljs-keyword">if</span> (err) {
				<span class="hljs-keyword">return</span> setImmediate(cb, err);
			}

			<span class="hljs-keyword">if</span> (!account || !account.publicKey) {
				<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Account not found'</span>);
			}

			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">publicKey</span>: account.publicKey});
		});
	});
};

shared.getDelegates = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	library.schema.validate(req.body, schema.getDelegates, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err[<span class="hljs-number">0</span>].message);
		}

		self.getAccount({ <span class="hljs-attr">address</span>: req.body.address }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, account</span>) </span>{
			<span class="hljs-keyword">if</span> (err) {
				<span class="hljs-keyword">return</span> setImmediate(cb, err);
			}

			<span class="hljs-keyword">if</span> (!account) {
				<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Account not found'</span>);
			}

			<span class="hljs-keyword">if</span> (account.delegates) {
				modules.delegates.getDelegates(req.body, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, res</span>) </span>{
					<span class="hljs-keyword">var</span> delegates = res.delegates.filter(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">delegate</span>) </span>{
						<span class="hljs-keyword">return</span> account.delegates.indexOf(delegate.publicKey) !== <span class="hljs-number">-1</span>;
					});

					<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">delegates</span>: delegates});
				});
			} <span class="hljs-keyword">else</span> {
				<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">delegates</span>: []});
			}
		});
	});
};

shared.getDelegatesFee = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">fee</span>: constants.fees.delegate});
};

shared.addDelegates = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	library.schema.validate(req.body, schema.addDelegates, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err[<span class="hljs-number">0</span>].message);
		}

		<span class="hljs-keyword">var</span> keypair = library.ed.makeKeypair(req.body.secret);

		<span class="hljs-keyword">if</span> (req.body.publicKey) {
			<span class="hljs-keyword">if</span> (keypair.publicKey.toString(<span class="hljs-string">'hex'</span>) !== req.body.publicKey) {
				<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid passphrase'</span>);
			}
		}

		library.balancesSequence.add(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
			<span class="hljs-keyword">if</span> (req.body.multisigAccountPublicKey &amp;&amp; req.body.multisigAccountPublicKey !== keypair.publicKey.toString(<span class="hljs-string">'hex'</span>)) {
				modules.accounts.getAccount({ <span class="hljs-attr">publicKey</span>: req.body.multisigAccountPublicKey }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, account</span>) </span>{
					<span class="hljs-keyword">if</span> (err) {
						<span class="hljs-keyword">return</span> setImmediate(cb, err);
					}

					<span class="hljs-keyword">if</span> (!account || !account.publicKey) {
						<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Multisignature account not found'</span>);
					}

					<span class="hljs-keyword">if</span> (!account.multisignatures || !account.multisignatures) {
						<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Account does not have multisignatures enabled'</span>);
					}

					<span class="hljs-keyword">if</span> (account.multisignatures.indexOf(keypair.publicKey.toString(<span class="hljs-string">'hex'</span>)) &lt; <span class="hljs-number">0</span>) {
						<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Account does not belong to multisignature group'</span>);
					}

					modules.accounts.getAccount({ <span class="hljs-attr">publicKey</span>: keypair.publicKey }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, requester</span>) </span>{
						<span class="hljs-keyword">if</span> (err) {
							<span class="hljs-keyword">return</span> setImmediate(cb, err);
						}

						<span class="hljs-keyword">if</span> (!requester || !requester.publicKey) {
							<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Requester not found'</span>);
						}

						<span class="hljs-keyword">if</span> (requester.secondSignature &amp;&amp; !req.body.secondSecret) {
							<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Missing requester second passphrase'</span>);
						}

						<span class="hljs-keyword">if</span> (requester.publicKey === account.publicKey) {
							<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid requester public key'</span>);
						}

						<span class="hljs-keyword">var</span> secondKeypair = <span class="hljs-literal">null</span>;

						<span class="hljs-keyword">if</span> (requester.secondSignature) {
							secondKeypair = library.ed.makeKeypair(req.body.secondSecret);
						}

						<span class="hljs-keyword">var</span> transaction;

						<span class="hljs-keyword">try</span> {
							transaction = library.logic.transaction.create({
								<span class="hljs-attr">type</span>: transactionTypes.VOTE,
								<span class="hljs-attr">votes</span>: req.body.delegates,
								<span class="hljs-attr">sender</span>: account,
								<span class="hljs-attr">keypair</span>: keypair,
								<span class="hljs-attr">secondKeypair</span>: secondKeypair,
								<span class="hljs-attr">requester</span>: keypair
							});
						} <span class="hljs-keyword">catch</span> (e) {
							<span class="hljs-keyword">return</span> setImmediate(cb, e.toString());
						}

						library.bus.message(<span class="hljs-string">"transactionsReceived"</span>, [transaction], <span class="hljs-string">"api"</span>, cb);
					});
				});
			} <span class="hljs-keyword">else</span> {
				self.setAccountAndGet({ <span class="hljs-attr">publicKey</span>: keypair.publicKey.toString(<span class="hljs-string">'hex'</span>) }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, account</span>) </span>{
					<span class="hljs-keyword">if</span> (err) {
						<span class="hljs-keyword">return</span> setImmediate(cb, err);
					}

					<span class="hljs-keyword">if</span> (!account || !account.publicKey) {
						<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Account not found'</span>);
					}

					<span class="hljs-keyword">if</span> (account.secondSignature &amp;&amp; !req.body.secondSecret) {
						<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid second passphrase'</span>);
					}

					<span class="hljs-keyword">var</span> secondKeypair = <span class="hljs-literal">null</span>;

					<span class="hljs-keyword">if</span> (account.secondSignature) {
						secondKeypair = library.ed.makeKeypair(req.body.secondSecret);
					}

					<span class="hljs-keyword">var</span> transaction;

					<span class="hljs-keyword">try</span> {
						transaction = library.logic.transaction.create({
							<span class="hljs-attr">type</span>: transactionTypes.VOTE,
							<span class="hljs-attr">votes</span>: req.body.delegates,
							<span class="hljs-attr">sender</span>: account,
							<span class="hljs-attr">keypair</span>: keypair,
							<span class="hljs-attr">secondKeypair</span>: secondKeypair
						});
					} <span class="hljs-keyword">catch</span> (e) {
						<span class="hljs-keyword">return</span> setImmediate(cb, e.toString());
					}

					library.bus.message(<span class="hljs-string">"transactionsReceived"</span>, [transaction], <span class="hljs-string">"api"</span>, cb);
				});
			}
		}, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, transaction</span>) </span>{
			<span class="hljs-keyword">if</span> (err) {
				<span class="hljs-keyword">return</span> setImmediate(cb, err);
			}

			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {<span class="hljs-attr">transaction</span>: transaction[<span class="hljs-number">0</span>]});
		});
	});
};

shared.getAccount = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, cb</span>) </span>{
	library.schema.validate(req.body, schema.getAccount, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
		<span class="hljs-keyword">if</span> (err) {
			<span class="hljs-keyword">return</span> setImmediate(cb, err[<span class="hljs-number">0</span>].message);
		}

		<span class="hljs-keyword">var</span> isAddress = <span class="hljs-regexp">/^[1-9A-Za-z]{1,35}$/g</span>;
		<span class="hljs-keyword">if</span> (!isAddress.test(req.body.address)) {
			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Invalid address'</span>);
		}

		self.getAccount({ <span class="hljs-attr">address</span>: req.body.address }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, account</span>) </span>{
			<span class="hljs-keyword">if</span> (err) {
				<span class="hljs-keyword">return</span> setImmediate(cb, err);
			}

			<span class="hljs-keyword">if</span> (!account) {
				<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-string">'Account not found'</span>);
			}

			<span class="hljs-keyword">return</span> setImmediate(cb, <span class="hljs-literal">null</span>, {
				<span class="hljs-attr">account</span>: {
					<span class="hljs-attr">address</span>: account.address,
					<span class="hljs-attr">unconfirmedBalance</span>: account.u_balance,
					<span class="hljs-attr">balance</span>: account.balance,
					<span class="hljs-attr">publicKey</span>: account.publicKey,
					<span class="hljs-attr">unconfirmedSignature</span>: account.u_secondSignature,
					<span class="hljs-attr">secondSignature</span>: account.secondSignature,
					<span class="hljs-attr">secondPublicKey</span>: account.secondPublicKey,
					<span class="hljs-attr">multisignatures</span>: account.multisignatures || [],
					<span class="hljs-attr">u_multisignatures</span>: account.u_multisignatures || []
				}
			});
		});
	});
};</pre></div></div>
            
        </li>
        
        
        <li id="section-19">
          
            <div class="annotation">
          
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              <p>Export</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-built_in">module</span>.exports = Accounts;</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
